function isEmpty(obj) {
    if (typeof obj === 'undefined' || obj == null || obj === '') {
        return true;
    } else {
        return false;
    }
}

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: false,
        title: null,
        weiXinUrl: "",
        disabled: false,
        bArticleInfo: {}
    },
    methods: {
        getWeiXinPage: function (event) {
            vm.weiXinUrl = $('#weiXinUrl').val();
            if (vm.weiXinUrl == null || vm.weiXinUrl == '') {
                return false;
            }

            var url = "sys/bvideoinfo/getWeiXinPage";
            vm.disabled = !vm.disabled;

            var loading = layer.msg('文章拼命生成中，别催我...',{icon:16,shade:0.3,time:0})

            var url = "sys/bvideoinfo/getWeiXinPage";

            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(vm.weiXinUrl),
                dataType: "JSON",
                success: function (r) {
                    if (r.code === 0) {
                        vm.bArticleInfo = r.bArticleInfo;
                        console.info(vm.bArticleInfo);
                        window.current_editor.setContent(r.bArticleInfo.articleContent);
                    } else {
                        alert(r.msg);
                    }
                    layer.close(loading);
                }
            });

            vm.disabled = !vm.disabled;
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bArticleStand = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.bArticleInfo.id == null ? "sys/barticleinfo/save" : "sys/barticleinfo/save";

            vm.bArticleInfo.brandName = $('#articleBrandName').val();
            vm.bArticleInfo.articleTypeName=$('#articleTypeName').val();

            vm.bArticleInfo.articleContent = window.current_editor.getContent();
            //console.log("bArticleInfo Save:"+vm.bArticleInfo);
            if(vm.bArticleInfo.brandName == "" && vm.bArticleInfo.articleTypeName== ""){
                alert("请选择品牌或者分类,注意只能选择其中一项");
                return;
            }

            if(vm.bArticleInfo.brandName != "" && vm.bArticleInfo.articleTypeName != ""){
                alert("品牌或者分类,注意只能选择其中一项");
                return;
            }

            vm.bArticleInfo.articleTitle = $("#articleTitle").val();
            console.info(vm.bArticleInfo.articleDesc)
            if(!vm.bArticleInfo.articleDesc || vm.bArticleInfo.articleDesc == null || vm.bArticleInfo.articleDesc == ""){
                vm.bArticleInfo.articleDesc = vm.bArticleInfo.articleTitle
            }
            console.info(vm.bArticleInfo.articleDesc)

            vm.bArticleInfo.articleContent = window.current_editor.getContent();

            if(vm.bArticleInfo.articleTitle == "" || vm.bArticleInfo.articleDesc == "" || vm.bArticleInfo.articleContent == ""){
                alert("文章标题、概述、内容不能为空！");
                return;
            }

            console.info(vm.bArticleInfo)
            var loading = layer.msg('文章拼命保存中，别催我...',{icon:16,shade:0.3,time:0})
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=UTF-8",
                data:JSON.stringify(vm.bArticleInfo),
                dataType: "JSON",
                success: function(r){
                    layer.close(loading);
                    if(r.code === 0){
                        alert('保存成功！', function(index){
                            // window.location.href="../sys/webspider.html";
                            // location.reload();
                            if (r.resultId>0)
                                window.location.href="../sys/barticle_edit.html?id="+r.resultId;
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        closeInfo: function(){
            confirm('亲，您确定要关闭本页吗？', function () {
                window.opener = null;
                window.open('', '_self');
                window.close();
                vm.bArticleInfo = {};
            })
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/barticlestand/info/" + id, function (r) {
                vm.bArticleStand = r.bArticleStand;

                vm.bArticleStand.startTime = timeSet(r.bArticleStand.startTime);
                vm.bArticleStand.endTime = timeSet(r.bArticleStand.endTime);

                function timeSet(time) {
                    let myDate = new Date(time)
                    let curTime = myDate.getFullYear() + "-" + fix((myDate.getMonth() + 1), 2) + "-" + fix(myDate.getDate(), 2) + "T" + fix(myDate.getHours(),
                        2) + ":" + fix(myDate.getMinutes(), 2);
                    return curTime;
                }

                //将日期格式化为两位，不足补零
                function fix(num, length) {
                    return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;
                }


                vm.bArticleStand.startTime.replace(" ", "T")
                vm.bArticleStand.endTime.replace(" ", "T")
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});
